package org.xyxy.scientificResearch.web;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.xyxy.scientificResearch.entity.Notice;
import org.xyxy.scientificResearch.service.NoticeService;
import org.apache.struts2.convention.annotation.Result;
import org.hpin.base.usermanager.entity.User;
import org.hpin.common.core.SpringTool;
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.util.StrUtils;
import org.hpin.common.widget.pagination.Page;

@Namespace("/resource")
@Action("noticeAction")
@Results({
	@Result(name = "addNotice", location = "/WEB-INF/pages/notice/addNotice.jsp"),
	@Result(name = "browNoticeAll", location = "/WEB-INF/pages/notice/browNoticeAll.jsp"),
	@Result(name = "modifyNoticeAll", location = "/WEB-INF/pages/notice/modifyNoticeAll.jsp"),
	@Result(name = "modifyNotice", location = "/WEB-INF/pages/notice/modifyNotice.jsp"),
	@Result(name = "browNotice", location = "/WEB-INF/pages/notice/browNotice.jsp"),
	@Result(name = "listNotice", location = "/WEB-INF/pages/notice/listNotice.jsp")
	})
public class NoticeAction extends BaseAction{

	
	NoticeService noticeService=(NoticeService) SpringTool.getBean(NoticeService.class);
	private Notice notice;
	public String addNotice(){
		if(StrUtils.isNotNullOrBlank(id)){
			notice = (Notice) noticeService.findById(id);
			HttpTool.setAttribute("Notice", notice);
			
		}else{
			notice = new Notice();
		}
		return "addNotice";
		
	}
	/** 
	 * @列表
	 */
	public String listNotice() throws ParseException{
		page = new Page(HttpTool.getPageNum() , HttpTool.getPageSize());
		Map paramsMap = buildSearch();
		String orderField  = HttpTool.getParameter("orderField");
		String orderDirection = HttpTool.getParameter("orderDirection");
		if( paramsMap.containsKey("orderDirection")){
			paramsMap.remove("orderDirection");
		}
		if( paramsMap.containsKey("orderField")){
			paramsMap.remove("orderField");
		}
		if (StrUtils.isNotNullOrBlank(orderField)&&StrUtils.isNotNullOrBlank(orderDirection)) {
		//	paramsMap.put(""+orderField+"",orderDirection);
			paramsMap.put(orderField,orderDirection);
		}
		else {
			paramsMap.put("order_createTime", "desc");
		}
		HttpTool.setAttribute("orderField", orderField);
		HttpTool.setAttribute("orderDirection", orderDirection);
		findByPage(page, paramsMap);
		return "listNotice";
	}
	public Page findByPage(Page page , Map paramsMap){
		paramsMap.put("filter_and_isDeleted_EQ_I",0);
		paramsMap.put("order_createTime", "desc");
		 noticeService.findByPage(page, paramsMap);
		return page ;
	}
	/**
	 * @删除
	 * @throws IOException
	 */
	public String deleteNotice() throws IOException{
		JSONObject json = new JSONObject();
		try {
			User currentUser = (User)HttpTool.getSession().getAttribute("currentUser");
			String[] id = ids.replaceAll(" ", "").split(",");
			noticeService.deleteNotice(id,currentUser.getAccountName(),new Date());
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("callbackType", "");
			json.accumulate("forwardUrl", "");
			json.accumulate("confirmMsg", "");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		
		renderJson(json);
		return null;
	}	
	
	/** @添加基本信息*/
	public String saveNotice(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			User currentUser = (User)HttpTool.getSession().getAttribute("currentUser");
			if(StrUtils.isNotNullOrBlank(notice.getId())){
				if(null!=notice){
					System.out.println(notice.getId());
					notice.setUpdateTime(new Date());
					notice.setUpdateUserId(currentUser.getId());
					noticeService.update(notice);
					json.accumulate("statusCode", 200);
					json.accumulate("message", "操作成功");
					json.accumulate("navTabId", super.navTabId);
					json.accumulate("currentTabId", currentTabId);
					json.accumulate("callbackType", "closeCurrent");
				}
			}else{
				System.out.println(notice.getAttachment());
				notice.setId(null);
				notice.setCreateTime(new Date());
				notice.setCreateUserId(currentUser.getId());
				notice.setIsDeleted(0);
				notice.setStatus("1011701");
				System.out.println(notice.getReleaseTime());
				noticeService.save(notice);
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
				}
				
				
				
//		json.accumulate("rel", "");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", notice.getId());
		renderJson(json);
		return null;
	}
	public String browNoticeAll() throws ParseException{
		String flag = HttpTool.getParameter("flag");
		//String taskNo = HttpTool.getParameter("taskNo");
		HttpTool.setAttribute("id", id);
		HttpTool.setAttribute("flag", flag);
		//HttpTool.setAttribute("taskNo", taskNo);
		return "browNoticeAll";
	}
	/**
	 * @产看详细信息
	 */
	public String browNotice() throws ParseException{
		String flag = HttpTool.getParameter("flag");
		notice = (Notice)noticeService.findById(id);
		HttpTool.setAttribute("flag", flag);
		return "browNotice";
	}
	/**
	 * @修改
	 */
	public String modifyNoticeAll() throws ParseException{
		HttpTool.setAttribute("id", id);
		return "modifyNoticeAll";
	}
	/**
	 * @变更页面
	 */
	public String modifyNotice() throws ParseException{
		notice = (Notice)noticeService.findById(id);
		return "modifyNotice" ;
	}
	public Notice getNotice() {
		return notice;
	}
	public void setNotice(Notice notice) {
		this.notice = notice;
	}
		

}
